package com.gopay.bis.ceb.expay.operation;

import com.gopay.common.cipher.utils.LoggerMaskUtils;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.csii.payment.client.core.MerchantXmlSignVerify;
import com.gopay.bis.common.util.BankLogUtils;
import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.ceb.expay.CebExPayMessage;
import com.gopay.common.exception.GopayException;
import com.gopay.remote.bank.ICertHelper;
import org.springframework.beans.BeanUtils;

/**
 * 
 * @author lilili
 * @CebExPayCertHelper.java
 *
 */
public class CebExPayCertHelper implements ICertHelper {

    private static Log logger = LogFactory.getLog(CebExPayCertHelper.class);
    
	@Override
	public String sign(BaseMessage bsaeMessage) throws GopayException {
		 String signMsg=null;
	     try {
	        CebExPayMessage message = (CebExPayMessage)bsaeMessage;
	        BankLogUtils.printExPaySignSendLog(message);
	        signMsg = MerchantXmlSignVerify.merchantSignXmlData_ABA(message.getPlainMessage(),message.getTransId());
	        message.setSignMessage(signMsg);
			// + hougn 2017-11-21 日志敏感信息掩码  start
			logger.error("CEB EXPAY certhelper sign:"+ LoggerMaskUtils.addHeadMaskString(signMsg));
			//BankLogUtils.printExPaySignReturnLog(message, signMsg);
			// + hougn 2017-11-21 日志敏感信息掩码  end

	        return signMsg;
	    } catch (Exception e) {
	        BankLogUtils.printExPaySignExceptionLog(bsaeMessage, e);
	        throw new GopayException(ProcCodeConstants.PROC_CODE_100E6057);
	    }
	}
	@Override
	public Object verify(BaseMessage bsaeMessage) throws GopayException {
		boolean isVerfy=false;
        try {
        	CebExPayMessage message = (CebExPayMessage)bsaeMessage;

			// + hougn 2017-11-21 日志敏感信息掩码  start
			CebExPayMessage logMessage = new CebExPayMessage();
			BeanUtils.copyProperties(message, logMessage);
			String logXml = LoggerMaskUtils.addHeadMaskString(message.getSignMessage());
			logMessage.setSignMessage(logXml);
			BankLogUtils.printExPayVerifySendLog(logMessage);
			// + hougn 2017-11-21 日志敏感信息掩码  end

            isVerfy = MerchantXmlSignVerify.merchantVerifyXmlData_ABA(message.getSignMessage());
            BankLogUtils.printExPayVerifyResultLog(message, isVerfy);
            return isVerfy;
        } catch (Exception e) {
            BankLogUtils.printExPayVerifyExceptionLog(bsaeMessage, e);
            throw new GopayException(ProcCodeConstants.PROC_CODE_100E6057);
        }
	}
	
}
